import { defineFakeRoute } from "vite-plugin-fake-server/client";
import { faker } from "@faker-js/faker/locale/zh_CN";

export default defineFakeRoute([
  // 文章分类列表
  {
    url: "/api/article-category/list",
    method: "post",
    response: ({ query }) => {
      const { page = 1, pageSize = 10 } = query;
      const name = query.name as string;
      const list = Array.from({ length: 10 }).map((_, index) => ({
        id: index + 1,
        name: faker.lorem.word(),
        parent_id: index % 3 === 0 ? null : faker.number.int({ min: 1, max: 3 }),
        children: [],
        sort: faker.number.int({ min: 1, max: 100 }),
        icon: faker.helpers.arrayElement(["ri:home-line", "ri:book-line", "ri:folder-line"]),
        display: faker.datatype.boolean(),
        created_at: faker.date.past().toLocaleString(),
        updated_at: faker.date.past().toLocaleString()
      }));

      let filteredList = list;
      if (name) {
        filteredList = filteredList.filter(item => item.name.includes(name));
      }

      const total = filteredList.length;
      const paginatedList = filteredList.slice(
        (Number(page) - 1) * Number(pageSize),
        Number(page) * Number(pageSize)
      );

      return {
        success: true,
        code: 200,
        data: {
          list: paginatedList,
          total: total
        },
        msg: "获取列表成功"
      };
    }
  },
  // 添加文章分类
  {
    url: "/api/article-category/add",
    method: "post",
    response: () => {
      return {
        success: true,
        code: 200,
        data: {},
        msg: "添加成功"
      };
    }
  },
  // 修改文章分类
  {
    url: "/api/article-category/update",
    method: "post",
    response: () => {
      return {
        success: true,
        code: 200,
        data: {},
        msg: "修改成功"
      };
    }
  },
  // 删除文章分类
  {
    url: "/api/article-category/delete",
    method: "post",
    response: () => {
      return {
        success: true,
        code: 200,
        data: {},
        msg: "删除成功"
      };
    }
  },
  // 获取文章分类树
  {
    url: "/api/article-category/tree",
    method: "get",
    response: () => {
      const list = Array.from({ length: 5 }).map((_, index) => ({
        id: index + 1,
        name: faker.lorem.word(),
        parent_id: null,
        children: Array.from({ length: 2 }).map((_, childIndex) => ({
          id: (index + 1) * 10 + childIndex + 1,
          name: faker.lorem.word(),
          parent_id: index + 1,
          sort: faker.number.int({ min: 1, max: 100 }),
          icon: faker.helpers.arrayElement(["ri:home-line", "ri:book-line", "ri:folder-line"]),
          display: faker.datatype.boolean(),
          created_at: faker.date.past().toLocaleString(),
          updated_at: faker.date.past().toLocaleString()
        })),
        sort: faker.number.int({ min: 1, max: 100 }),
        icon: faker.helpers.arrayElement(["ri:home-line", "ri:book-line", "ri:folder-line"]),
        display: faker.datatype.boolean(),
        created_at: faker.date.past().toLocaleString(),
        updated_at: faker.date.past().toLocaleString()
      }));
      return {
        success: true,
        code: 200,
        data: {
          list: list
        }
      };
    }
  }
]);
